package com.saga.dim.app;

import com.saga.dim.bean.DimDateBean;
import com.saga.dim.util.DimDateUtils;
import com.alibaba.fastjson.JSON;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import java.io.IOException;
import java.util.List;

public class DimDateApp {

    private static String APP_ID = "skgsrtrlst89tfjl";
    private static String APP_SECRET = "UXhiazBRNzh6OXU3WTFuSnIzY2QxZz09";
    private static String YEAR = "2022";

    public static void main(String[] args) {

//        String url = "https://www.mxnzp.com/api/holiday/list/year/" + YEAR + "?ignoreHoliday=false&app_id=" + APP_ID + "&app_secret=" + APP_SECRET;
        String url = "http://10.5.26.171/data-api/query/423?user_code=001506";
        try {
            getParamMap(url);
//            List<DimDateBean> dateInfoByURL = DimDateUtils.getDateInfoByURL(url, DimDateBean.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static JSONObject getParamMap(String url) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            CloseableHttpResponse response;
            URIBuilder builder = new URIBuilder(url);
            // 执行get请求.
            HttpGet httpGet = new HttpGet(builder.build());
            //"Content-Type=application/json;charset=UTF-8"
//            httpGet.setHeader("Content-Type", "x-www-form-urlencoded");
            httpGet.setHeader("charset", "UTF-8");
            response = httpClient.execute(httpGet);
            if(response != null && response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                String jsonString= EntityUtils.toString(entity);
                JSONObject jsonObject=new JSONObject(jsonString);
                System.out.println("get请求数据成功！\n"+jsonObject);
                return jsonObject;
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
